package com.un10.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.un10.entity.Administrator;
import com.un10.entity.User;

public class qiantaiLoginInterceptor implements HandlerInterceptor{

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
		// TODO Auto-generated method stub
		  //��ȡ�����RUi:ȥ��http:localhost:8080�ⲿ��ʣ�µ�
		  String uri = request.getRequestURI();
		  //UTL:����login.jsp�ǿ��Թ������ʵģ�������URL���������ؿ���
		  if (uri.indexOf("/login") >= 0) {
		  return true;
		  }
		  //��ȡsession
		  HttpSession session = request.getSession();
		  User user = (User) session.getAttribute("USER_SESSION");
		  //�ж�session���Ƿ����û����ݣ�����У��򷵻�true����������ִ��
		  if (user != null) {
		  //return true�������
		  return true;
		  }
		  //�����������ĸ�����ʾ��Ϣ����ת������¼ҳ��
		  request.setAttribute("msg", "����û�е�¼�����ȵ�¼��");
		  request.getRequestDispatcher("/B/login.jsp").forward(request, response);
		
		  //return false��������
		  return false;
	}

}
